<?php

/**
 * This is the model class for table "{{groups}}".
 *
 * The followings are the available columns in table '{{groups}}':
 * @property integer $id
 * @property string $name
 * @property integer $user_id
 * @property integer $avatar
 * @property string $description
 * @property string $website
 * @property string $category
 * @property string $city
 * @property string $status
 */
class Groups extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Groups the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{groups}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name', 'required'),
			array('user_id, country_id', 'numerical', 'integerOnly'=>true),/*invitation, */
			array('name, avatar, website, city, status, category', 'length', 'max'=>255),
			array('user_id, description, approve', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, user_id, avatar, description, website, category, city, status', 'safe', 'on'=>'search'),/*invitation, */
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			// Создатель
			'user'=>array(self::BELONGS_TO, 'Users', 'user_id'),
			// Подписчики
			'countSubscribers' 	=> 	array(self::STAT, 'GroupsUsers', 'group_id',
				'condition'	=>	'invitation=0 AND status=1'
			),
			'subscribers' 		=> 	array(self::HAS_MANY, 'GroupsUsers', 'group_id',
				'condition'	=>	'invitation=0 AND subscribers.status=1'
			),
			'subscribersUsers'	=>	array(self::HAS_MANY,'Users',array('user_id'=>'id'),
				'through'	=>	'subscribers',
				'select'	=>	'id, full_name, avatar',
				'condition'	=>	'subscribers.invitation=0 AND subscribers.status=1',
				'limit' 	=> 	6,
				'order' 	=>	'RAND()',
			),
			'UsersAll'			=>	array(self::HAS_MANY,'Users',array('user_id'=>'id'),
				'through'	=>	'subscribers',
				'select'	=>	'id, full_name, avatar',
				'condition'	=>	'invitation=0 AND subscribers.status=1',
				'order' 	=>	'full_name',
			),
			'UsersAdmins'			=>	array(self::HAS_MANY,'Users',array('user_id'=>'id'),
				'through'	=>	'subscribers',
				'select'	=>	'id, full_name, avatar',
				'condition'	=>	'invitation=0 AND subscribers.status=1 AND subscribers.admin=1',
				'order' 	=>	'full_name',
			),
			'UsersAllUpdate'	=>	array(self::HAS_MANY,'Users',array('user_id'=>'id'),
				'through'	=>	'subscribers',
				'select'	=>	'id, full_name, avatar',
				'condition'	=>	'invitation=0 AND subscribers.status=1',
				'order' 	=>	'subscribers.id',
			),
			'checkUser' 		=> 	array(self::HAS_ONE, 'GroupsUsers', 'group_id',
				'condition'	=>	'user_id=:user AND invitation=0',
				'params'	=>	array(':user'=>Yii::app()->user->id)
			),
			// Заявки
			'countRequest'		=> 	array(self::STAT, 'GroupsUsers', 'group_id',
				'condition'	=>	'invitation=0 AND status=0'
			),
			'subscribersRequest' 		=> 	array(self::HAS_MANY, 'GroupsUsers', 'group_id',
				'condition'	=>	'invitation=0 AND subscribersRequest.status=0'
			),
			'UsersRequest'		=>	array(self::HAS_MANY,'Users',array('user_id'=>'id'),
				'through'	=>	'subscribersRequest',
				'select'	=>	'id, full_name, avatar',
				'condition'	=>	'invitation=0 AND subscribersRequest.status=0',
				'order' 	=>	'subscribersRequest.id',
			),
			// 
			'countInvitation' 	=> 	array(self::STAT, 'GroupsUsers', 'group_id',
				'condition'	=>	'invitation=1'
			),
			'subscribersInvitation'=> 	array(self::HAS_MANY, 'GroupsUsers', 'group_id',
				'condition'	=>	'invitation=1'
			),
			// Контакты
			'countContacts' 	=> 	array(self::STAT, 'GroupsUsers', 'group_id',
				'condition'	=>	'ruler=:ruler AND invitation=0',
				'params' 	=> 	array(':ruler'=>true),
			),
			'contacts' 			=> 	array(self::HAS_MANY, 'GroupsUsers', 'group_id',
				'condition'	=>	'ruler=:ruler AND invitation=0',
				'params' 	=> 	array(':ruler'=>true),
			),
			'checkAdmin'		=>	array(self::HAS_ONE, 'GroupsUsers', 'group_id',
				'condition'	=>	'admin=:admin AND user_id=:user AND invitation=0',
				'params' 	=> 	array(':admin'=>true,'user'=>Yii::app()->user->id),
			),
			'checkComment'		=>	array(self::HAS_ONE, 'GroupsUsers', 'group_id',
					'condition'	=>	'writecomment=:writecomment AND user_id=:user AND invitation=0',
					'params' 	=> 	array(':writecomment'=>true,'user'=>Yii::app()->user->id),
			),
			'contactsUsers'		=>	array(self::HAS_MANY,'Users',array('user_id'=>'id'),
				'through'	=>	'contacts',
				'select'	=>	'id, full_name, avatar',
				'condition'	=>	'ruler=:ruler AND invitation=0',
				'params' 	=> 	array(':ruler'=>true),
				'order' 	=> 	'contacts.id',
			),
			// Ссылки
			'countUrls'			=>	array(self::STAT, 'GroupsUrls', 'group_id'),
			'urls'				=>	array(self::HAS_MANY, 'GroupsUrls', 'group_id',
				'limit' 	=> 	5,
				'order'		=>	'id DESC',
			),
			'urlsAll'				=>	array(self::HAS_MANY, 'GroupsUrls', 'group_id',
				'order'		=>	'id DESC',
			),
			// Мероприятия
			'countEvents'		=>	array(self::STAT, 'Events', 'group_id'),
			'events'			=>	array(self::HAS_MANY, 'Events', 'group_id',
				'limit' 	=>	2,
				'order' 	=>	'events.crdate DESC',
			),
			// Альбомы
			'avatar_info'=>array(self::BELONGS_TO, 'Photos', 'avatar'),	
			'countAlbums'		=>	array(self::STAT, 'Albums', 'group_id'),
			'albums'			=>	array(self::HAS_MANY, 'Albums', 'group_id'),
			// Музика
			'countMusics'=>array(self::STAT, 'MusicGroup', 'group_id'),
			'musics'=>array(self::HAS_MANY, 'MusicGroup', 'group_id',
				'limit' => 4,
				'order' => 'musics.id DESC',
			),
			// Videos
			'videos'=>array(self::HAS_MANY, 'VideoUser', 'group_id',
				'limit' => 10,
				'order' => 'RAND()',
			),
			'countVideos'=>array(self::STAT, 'VideoUser', 'group_id'),
			// Обсуждения
			'countDiscussion'	=>	array(self::STAT, 'GroupsDiscussion', 'group_id'),
			'discussions'		=>	array(self::HAS_MANY, 'GroupsDiscussion', 'group_id',
				'limit'		=>	3,
				'order'		=>	'crdate DESC',
			),
			'discussionsAll'		=>	array(self::HAS_MANY, 'GroupsDiscussion', 'group_id'),
			// Страна
			'country'			=>	array(self::BELONGS_TO, 'Country', 'country_id'),
			// Живая лента
			'countComments'		=>	array(self::STAT, 'GroupsComments', 'group_id'),
			'comments'			=>	array(self::HAS_MANY, 'GroupsComments', 'group_id',
				'limit' 	=> 	10,
				'order' 	=> 	'comments.id DESC',
				'condition'	=>	'comments.parent_id=0'
			),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Название',
			'user_id' => 'Создатель',
			'avatar' => 'Аватар',
			'description' => 'Описание сообщества',
			'website' => 'Веб-сайт',
			'category'=>'Категория',
			'country_id' => 'Страна',
			'city' => 'Город',
			'status' => 'Статус',
			'approve' => 'Тип группы',
		);
	}

	protected function beforeSave()
	{
		if(parent::beforeSave())
		{
			if($this->isNewRecord)
			{
				$this->user_id	=	Yii::app()->user->id;
				
			}
			return true;
		}
		else
			return false;
	}

	protected function beforeDelete()
	{
		if(parent::beforeDelete())
		{
			GroupsComments::model()->deleteAll(
				'group_id=:group_id',
				array(':group_id'=>$this->id)
			);
			Albums::model()->deleteAll(
				'group_id=:group_id',
				array(':group_id'=>$this->id)
			);
			Events::model()->deleteAll(
				'group_id=:group_id',
				array(':group_id'=>$this->id)
			);
			GroupsDiscussion::model()->deleteAll(
				'group_id=:group_id',
				array(':group_id'=>$this->id)
			);
			GroupsUrls::model()->deleteAll(
				'group_id=:group_id',
				array(':group_id'=>$this->id)
			);
			GroupsUsers::model()->deleteAll(
				'group_id=:group_id',
				array(':group_id'=>$this->id)
			);
			MusicGroup::model()->deleteAll(
				'group_id=:group_id',
				array(':group_id'=>$this->id)
			);
			VideoUser::model()->deleteAll(
				'group_id=:group_id',
				array(':group_id'=>$this->id)
			);
			return true;
		}
		else
			return false;
	}
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('user_id',$this->user_id);
		//$criteria->compare('invitation',$this->invitation,true);
		$criteria->compare('avatar',$this->avatar);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('website',$this->website,true);
		$criteria->compare('category',$this->category,true);
		$criteria->compare('country_id',$this->country_id,true);
		$criteria->compare('city',$this->city,true);
		$criteria->compare('status',$this->status,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
}